package com.renfei.multimediatask.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.media.MediaCodec;
import android.media.MediaFormat;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.TextureView;

import java.io.IOException;

public class Camera1Loader extends CameraLoader {
    private Activity activity;

    private Camera cameraInstance;
    private int cameraFacing = Camera.CameraInfo.CAMERA_FACING_BACK;

    private SurfaceHolder surfaceHolder;

    public Camera1Loader(Activity activity) {
        this.activity = activity;
    }



    @Override
    public void onCreate(int width, int height, OnPreviewSelectListener onPreviewSelectListener) {
        setUpdateCamera();
    }

    @Override
    public void startPreViewSurfaceHolder(SurfaceHolder surfaceHolder) {
        this.surfaceHolder = surfaceHolder;
        startPreview();
    }

    @Override
    public void startPreViewTextureView(SurfaceTexture surfaceTexture) {

    }


    private void startPreview() {
        try {
            if (null != surfaceHolder) {
                cameraInstance.setPreviewDisplay(surfaceHolder);
                cameraInstance.startPreview();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void onPause() {
        releaseCamera();
    }


    @Override
    public void swithCamera() {

        switch (cameraFacing) {
            case Camera.CameraInfo.CAMERA_FACING_FRONT:
                cameraFacing = Camera.CameraInfo.CAMERA_FACING_FRONT;
                break;
            case Camera.CameraInfo.CAMERA_FACING_BACK:
                cameraFacing = Camera.CameraInfo.CAMERA_FACING_BACK;
                break;
        }
        releaseCamera();
        setUpdateCamera();
        startPreview();
    }

    @Override
    public int getCameraOrientation() {
        int degrees = 0;

        switch (activity.getWindowManager().getDefaultDisplay().getOrientation()) {

            case Surface.ROTATION_0:
                degrees = 0;
                break;
            case Surface.ROTATION_90:
                degrees = 90;
                break;
            case Surface.ROTATION_180:
                degrees = 180;
                break;
            case Surface.ROTATION_270:
                degrees = 270;
                break;
        }
        if (cameraFacing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            degrees = (90 + degrees) % 360;
        } else { // back-facing
            degrees = (90 - degrees) % 360;
        }
        return degrees;
    }

    @Override
    public boolean hasMultipleCamera() {
        return Camera.getNumberOfCameras() > 1;
    }


    private void setUpdateCamera() {
        int currentCamerId = getCurrentCameraId();
        cameraInstance = getCameraInstance(currentCamerId);

        Camera.Parameters parameters = cameraInstance.getParameters();
        // 设置相机参数
        cameraInstance.setParameters(parameters);

        /**
         * camera1  默认返回格式为NV21
         */
        cameraInstance.setPreviewCallback(new Camera.PreviewCallback() {
            @Override
            public void onPreviewFrame(byte[] bytes, Camera camera) {
                if (null != onPreviewFrameListener) {
                    Camera.Size size = camera.getParameters().getPreviewSize();
                    onPreviewFrameListener.onPreviewFrame(
                            bytes, size.width, size.height
                    );
                }
            }
        });
    }

    private void releaseCamera() {
        cameraInstance.stopPreview();
        cameraInstance.setPreviewCallback(null);
        cameraInstance.release();
        cameraInstance = null;
    }


    private int getCurrentCameraId() {
        Camera.CameraInfo info = new Camera.CameraInfo();
        int count = Camera.getNumberOfCameras();
        for (int i = 0; i < count; i++) {
            Camera.getCameraInfo(i, info);
            if (info.facing == cameraFacing) {
                return i;
            }
        }
        return 0;
    }

    private Camera getCameraInstance(int id) {
        return Camera.open(id);
    }
}
